/**
 * Created by yyj on 2015/10/14.
 */
$(function(){
    $('.checkAll').click(function(){
        if($(this).is(':checked')){
            $('.content_table .checkinput').attr('checked','checked');
            $(this).attr('title','取消全选')
        }else{
            $('.content_table .checkinput').removeAttr('checked');
            $(this).attr('title','全选')
        }
    })
    //1 绑定添加按钮
    $('.add_btn').click(function(){
        //加载层
//        var index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
       index= layer.open({
       	extend:['skin/layer_iframe.css'],
        skin:'layer-ext-skin-layer_iframe.css',
            type:1,
            title:'添加类型页面',
            area:['400px','400px'],
            content:handlerTemplate({'name':'','desc':''}),
            maxmin: true,
            shadeClose: true
        });

        //4 重置
        $("#lookupType").focus();
        $(".resetBtn").click(function(){
            $("#lookupType").attr("value","");
            $("#lookupDesc").attr("value","");
        });

        save_func(this,"add");

    });
    //3 绑定编辑按钮
    $('.edit_btn').click(function(){
        edit_func(this);

        //4 重置
        $("#lookupType").focus();
        $(".resetBtn").click(function(){
            $("#lookupType").attr("value","");
            $("#lookupDesc").attr("value","");
        });
        save_func(this,'edit');
    });

    $(".viewAlarm").click(function(){
        var url = $(this).parent().attr("url");
        window.location.href=url;
    });

})
/**
 * 保存
 * @param obj
 * @param type  add edit
 */
save_func = function(obj,type){
    //1 字段验证
    $("#lookupType").blur(function () {
        $("#saveBtn").removeAttr("disabled");
        $("#lookupType").attr("style", "border-color: ;");
        $("#lookupType").attr("title", "");
        if ($.trim(this.value) == "") {
            $("#lookupType").attr("style", "border-color:red;");
            $("#lookupType").attr("title", "码表类型不能为空");
        }
        else {
            $("#lookupType").attr("style", "border-color: ;");
            $("#lookupType").attr("title", "");
        }
    });
    $("#lookupDesc").blur(function () {
        $("#saveBtn").removeAttr("disabled");
        $("#lookupDesc").attr("style", "border-color: ;");
        $("#lookupDesc").attr("title", "");
        if ($.trim(this.value) == "") {
            $("#lookupDesc").attr("style", "border-color:red;");
            $("#lookupDesc").attr("title", "类型描述不能为空");
        }
        else {
            $("#lookupDesc").attr("style", "border-color: ;");
            $("#lookupDesc").attr("title", "");
        }
    });
    $("#saveBtn").click(function () {
        var messagelookupType = $("#lookupType").attr("title");
        var messagelookupDesc = $("#lookupDesc").attr("title");
        if ( "" == messagelookupDesc && "" == messagelookupType) {
            var data= {};
            data['lookupDesc']=$("#lookupDesc").val();
            data['lookupType']=$("#lookupType").val();
            data['type']=type;
            $.ajax({
                url: "saveLookupType.do",
                type: "post",
                dataType: 'json',
                data: data,
                beforeSend: function () {
                    $("#saveBtn").attr("disabled", "disabled");
                    $("#tipMsg").html("正在保存中，请稍后......");
                },
                success: function (result) {
                    $("#tipMsg").html("");
                    if (result.data.flag) {
                        layer.msg('添加成功', {time: 2000});
                        parent.paging(1);
                        //parent.layer.close(parent.layer.getFrameIndex(window.name));
                        layer.closeAll();
                        setTimeout('parent.layer.close(' + index + ')', 2001);
                    } else {
                        $("#tipMsg").html("");
                        layer.msg('添加失败', {time: 2000});
                        $("#saveBtn").removeAttr("disabled");
                    }

                }
            });
        }else {
            var message="";
            if(messagelookupDesc!='')message=messagelookupDesc;
            if(messagelookupType!='')message=messagelookupType;
            layer.msg(message, {time: 2000});
        }
    });
}


edit_func=function(obj){
    var data={};
    var desc = $(obj).parent().prev().attr('title');
    var type = $(obj).parent().prev().prev().attr('title');
    data['name']=type;
    data['desc']=desc;
    index=layer.open({
    	extend:['skin/layer_iframe.css'],
        skin:'layer-ext-skin-layer_iframe.css',
        type:1,
        title:'修改码表类型页面',
        area:['400px','400px'],
        content:handlerTemplate(data),
        maxmin: true,
        shadeClose: true
    });
}

/**
 * 模版处理方法
 * @param data
 * @returns {*}
 */
handlerTemplate=function(data){
    //注册一个Handlebars模版，通过id找到某一个模版，获取模版的html框架
    var myTemplate = Handlebars.compile($("#lookupType-template").html());
    return myTemplate(data);
}
/**
 * 分页展示页
 * @param pageNo
 */
function paging(pageNo) {
    var pageSize = $("#pageSize  option:selected").text();
    $.ajax({
        url: "pagingLookupType.do",
        type: "post",
        dataType: 'json',
        data: {
            "iPageSize": pageSize,
            "iPageNo": pageNo
        },
        error: function (data) {
        	layer.msg('出错了！！',{time:2000});
        },
        success: function (jsons) {
            if (jsons != null) {
                $("#pageTable").html("");
                var pageList = jsons.pageData.pageList;
                for (var i = 0; i < pageList.length; i++) {
                    var partsForm =
                        '<tr codeid="'+pageList[i].lookupType +'" url="initlookupcde.do?lookupType='+pageList[i].lookupType+'">' +
                        //'<td class="checkNot"><input type="checkbox" value="' + pageList[i].lookuType + '" class="checkinput"/></td>' +
                        //  '<td class="checkNot"><input type="checkbox" onclick="manucheck(this);" value="'+pageList[i].ciTypeId+'" '+(pageList[i].canDeleteMsg!=''?'disabled':'name="pageItem"')+' class="checkinput"/></td>'+
                        // '<td class="checkNot"><input type="checkbox" name="checkradio"   value="'+pageList[i].lookupCode+'" class="checkinput" disabled="disabled"/></td>'+
                        '<td style="cursor:pointer" class="viewAlarm" title=' + (pageList[i].lookupType == null ? '' : pageList[i].lookupType) + '>' + (pageList[i].lookupType == null ? '' : pageList[i].lookupType) + '</td>' +
                        '<td style="cursor:pointer" class="viewAlarm"  title=' + (pageList[i].lookupDesc == null ? '' : pageList[i].lookupDesc) + '>' + (pageList[i].lookupDesc == null ? '' : pageList[i].lookupDesc) + '</td>' +
                        '<td><i class="Edit content_current edit_btn" title="编辑"></i></td>' +
                        '</tr>';
                    $(partsForm).appendTo("#pageTable");
                }

                //3 绑定编辑按钮
                $('.edit_btn').click(function(){
                    edit_func(this);

                    //4 重置
                    $("#lookupType").focus();
                    $(".resetBtn").click(function(){
                        $("#lookupType").attr("value","");
                        $("#lookupDesc").attr("value","");
                    });
                    save_func(this,"edit");
                });
                //4 点击跳转
                $(".viewAlarm").click(function(){
                    var url = $(this).parent().attr("url");
                    window.location.href=url;
                });

                //处理footer 分页
                var totalCount = jsons.pageData.totalCount;
                var pageSize = jsons.pageData.pageSize;
                var totalPage = jsons.pageData.totalPage;
                var pageNo = jsons.pageData.pageNo;
                dealFooterPage(totalCount, pageSize, totalPage, pageNo);
            }
        }
    });
}